<html>
    <head>       
        <style>          
        </style>
    </head>
    <body>
       <h1>Simple Input</h1>  
       <table>
           <tr>            
               <td  colspan="4" >
                   <input type="text" #input>
               </td>               
           </tr>
           <tr>
                <td>
                    <button .btn >1</button>
                </td>
                <td>
                    <button .btn >2</button>
                </td>    
                <td>
                    <button .btn >3</button>
                </td>   
                <td>
                    <button .btn >+</button>
                </td>            
            </tr>
            <tr>
                <td>
                    <button .btn >4</button>
                </td>
                <td>
                    <button .btn >5</button>
                </td>    
                <td>
                    <button .btn >6</button>
                </td>  
                <td>
                    <button .btn >-</button>
                </td>          
            </tr>  
            <tr>
                <td>
                    <button .btn >7</button>
                </td>
                <td>
                    <button .btn >8</button>
                </td>    
                <td>
                    <button .btn >9</button>
                </td>   
                <td>
                    <button .btn >*</button>
                </td>         
            </tr>  

            <tr>
                <td colspan="2">0</td>
                <td><button .btn>=</button></td>
                <td>
                    <button .btn >/</button>
                </td> 
            </tr>
       </table>
       <p> Previous Press <label for="" #prev></label></p>

       <script type="text/tiscript">
            
            var root = view.root;
            var current = 0;
            var result = 0;
            var opr = ""
            var prev  = "0"; 
            
            self.on("click",".btn", function(){
                  
                stdout.println("text", current);
;               current = self#input.text;

                switch (this.text){                  
                    case "+":{
                        // result = view.Operate(toInteger(current),toInteger(result),"+");
                        opr = "+"
                        self#input.text = ""   
                        prev = "+"                    
                        break;
                    }
                    case "-":{
                        // result = view.Operate(toInteger(current),toInteger(result),"-");
                        self#input.text = ""    
                        opr = "-"  
                        prev = "-"                 
                        break;
                    }
                    case "*":{
                        if (current == 0){
                            current = 1
                        }
                        // result = view.Operate(toInteger(current),toInteger(result),"*");
                        opr = "*"
                        self#input.text = ""       
                        prev = "*"                
                        break;
                    }
                    case "/":{
                        if (current == 0){
                            current = 1
                        }
                        // result = view.Operate(toInteger(current),toInteger(result),"/");
                        opr = "/"
                        self#input.text = ""     
                        prev = "/"                   
                        break;
                    }
                    case "=":{
                        // view.Operate(toInteger(current),toInteger(self#input.text),"=");   
                        current = self#input.text
                        if (opr != "_"){
                        result = view.Operate(toInteger(result),toInteger(current),opr);                      
                        self#input.text = result
                        opr = "_"
                        prev = "="          
                        }
                        break;
                    }
                    default:{
                        if (prev == "+" || prev == "-"  || prev == "/" 
                            || prev == "*"  ||prev == "=") {                      
                            // view.msgbox("resetting input box")
                            self#input.text = this.text       
                            prev = self#this.text                    
                        }else{                         
                            self#input.text = self#input.text + this.text;
                            prev = self#this.text
                        }
                    }
                }
                stdout.println("text", current);  
            })
       </script>

    </body>
</html>